package com.bicon.botu.finance.wallet.api;

import com.bicon.botu.finance.wallet.common.WalletConstants;
import com.bicon.botu.finance.wallet.entity.FinancialAccount;
import com.bicon.botu.finance.wallet.service.FinancialAccountService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.MatcherUtil;
import com.bicon.botu.core.base.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户支付宝，微信，银行卡管理
 *
 * @Author: sx
 * @Date: Created in 18:22 2017/6/21/021
 */
@RestController
@RequestMapping("/V1.0.0/api/financial")
public class FinancialAccountApi {

    @Autowired
    private FinancialAccountService financialAccountService;

    /**
     * 添加一个卡片管理
     *
     * @param userId
     * @param owner
     * @param financialNum
     * @param financialType
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam(value = "userId") String userId,
                          @RequestParam(value = "owner") String owner,
                          @RequestParam(value = "financialNum") String financialNum,
                          @RequestParam(value = "financialType") String financialType,
                          @RequestParam(value = "phone", required = false) String phone,
                          @RequestParam(value = "identityCard", required = false) String identityCard) {
        if (!(WalletConstants.FINANCIAL_TYPE_ALIPAY.equals(financialType)
                || WalletConstants.FINANCIAL_TYPE_WECHAT.equals(financialType)
                || WalletConstants.FINANCIAL_TYPE_BANK.equals(financialType))) {
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }
        if (!MatcherUtil.matcherPhone(phone)) {
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }

        return financialAccountService.add(userId, owner, financialNum, financialType, phone, identityCard);
    }

    /**
     * 删除一个卡片管理
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public BaseResult delete(@PathVariable(value = "id") String id) {
        return financialAccountService.delete(id);
    }

    /**
     * 修改
     * @param id
     * @param owner
     * @param financialNum
     * @param financialType
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam(value = "id") String id,
                             @RequestParam(value = "owner", required = false) String owner,
                             @RequestParam(value = "financialNum", required = false) String financialNum,
                             @RequestParam(value = "financialType", required = false) String financialType,
                             @RequestParam(value = "phone", required = false) String phone,
                             @RequestParam(value = "identityCard", required = false) String identityCard) {
        FinancialAccount financialAccount = financialAccountService.selectById(id);
        if (null==financialAccount){
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }
        if (StringUtils.isNotBlank(owner)) {
            financialAccount.setOwner(owner);
        }
        if (StringUtils.isNotBlank(financialNum)) {
            financialAccount.setFinancialNum(financialNum);
        }
        if (StringUtils.isNotBlank(financialType)) {
            if (!(WalletConstants.FINANCIAL_TYPE_ALIPAY.equals(financialType)
                    || WalletConstants.FINANCIAL_TYPE_WECHAT.equals(financialType)
                    || WalletConstants.FINANCIAL_TYPE_BANK.equals(financialType))) {
                return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
            }else {
                financialAccount.setFinancialType(financialType);
            }
        }
        if (StringUtils.isNotBlank(phone)) {
            if (MatcherUtil.matcherPhone(phone)){
                financialAccount.setFinancialNum(financialNum);
            }else {
                return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
            }
        }
        if (StringUtils.isNotBlank(identityCard)) {
            financialAccount.setIdentityCard(identityCard);
        }
        return financialAccountService.update(financialAccount);
    }

    /**
     * 查询所有一个卡片管理
     *
     * @param userId
     * @param financialType
     * @return
     */
    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    public BaseResult selectList(@RequestParam(value = "userId") String userId, @RequestParam(value = "financialType", required = false) String financialType) {
        return financialAccountService.selectList(userId, financialType);
    }
}
